<?php

namespace app\admin\service;

use app\admin\model\BoxDeviceRemain;
use app\admin\model\Member;

class BoxDeviceRemainService extends BaseService
{

    public function getList()
    {
        $param = request()->param();

        $map = [];
        $map[] = ['device_type', '=', $param['device_type'] ?? 0];

        if (!empty($param['time_range'])) {
            $map[] = ['time_day', '>=',$param['time_range'][0]];
            $map[] = ['time_day', '<=', $param['time_range'][1]];
        }
        $deviceRemain = new BoxDeviceRemain();
        $deviceInfos = $deviceRemain
            ->where($map)
            ->order('time_day', 'desc')
            ->page(PAGE, PERPAGE)
            ->select();

        foreach ($deviceInfos as $key => &$value) {
            //获取当日注册的用户
            for ($i = 2; $i <= 45; $i++) {
                $value[$i . 'day'] = empty($value['device_num']) ? 0 : round($value[$i.'day']/$value['device_num'] * 100, 2);
            }
        }

        //获取数据总数
        $count =$deviceRemain->where($map)->count();

        //返回结果
        return [
            "msg" => '操作成功',
            "code" => 0,
            "data" => $deviceInfos,
            "count" => $count,
        ];
    }

}